package cn.com.oceansoft.modules.usercenter;

import cn.com.oceansoft.application.Constant;
import cn.com.oceansoft.application.constant.ConfigInfo;
import cn.com.oceansoft.application.common.util.HttpClientUtil;
import cn.com.oceansoft.framework.kits.ApiResult;
import cn.com.oceansoft.modules.cache.service.BaseCacheService;
import cn.com.oceansoft.modules.members.entity.CompInfo;
import cn.com.oceansoft.modules.members.entity.Member;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.http.entity.ContentType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * Created by ganlu on 2016/10/3.
 */
@Controller
@RequestMapping("usercenter")
public class UserCenterController {
    @Resource
    protected ConfigInfo configInfo;
    @Resource
    private BaseCacheService cacheService;

    /**
    *功能：个人中心
    * */
    @RequestMapping("index")
    public String Index(HttpServletRequest request, Model model) throws IOException {

        Map mp_current_member = (Map) request.getSession().getAttribute(Constant.CURRENT_MEMBER);
        String result2 = HttpClientUtil.doGet(configInfo.getMsHostUrl() + "members/"+mp_current_member.get("guid")+"/att");
        ApiResult apiResult2 = JSONObject.parseObject(result2, ApiResult.class);
        if(apiResult2.getData()==null)
            model.addAttribute("pic","");
        else
            model.addAttribute("pic",apiResult2.getData().toString());
        String to=request.getParameter("to");
        if(StringUtils.isEmpty(to)){
            to="tasks";
        }
        model.addAttribute("active",to);
        return "usercenter/index";
    }
    /**
    *功能：个人中心
    * */
    @RequestMapping("information")
    public String information(Model model) {
        return "usercenter/informationCenter";
    }

    @RequestMapping("emspay")
    public String emspay(Model model) {
        return "usercenter/payems";
    }
    @RequestMapping(value = "tasks", method = GET)
    public String tasks(){
        return "usercenter/tasks";
    }

    @ResponseBody
    @RequestMapping(value = "task-list", method = GET)
    public ApiResult getTaskListByName(HttpServletRequest request) {
            String ps = request.getParameter("ps");
            String pi = request.getParameter("pi");
            String tskReserve=request.getParameter("tsk_reserve");
            String projectStatus =request.getParameter("projectStatus");
            String status =request.getParameter("status");
            String tskNum=request.getParameter("tsk_num");
            Map map= (Map) request.getSession().getAttribute(Constant.CURRENT_MEMBER);
            String creator = map.get("guid").toString();
            String result = HttpClientUtil.doGet(configInfo.getMsHostUrl() + "tasks/page",
                    "ps", ps, "pi", pi,"sb", "creatordate", "sd", "desc", "q_projectstatus_n_eq", projectStatus,"q_projectstatus_n_in", status,"q_tskReserve_s_eq",tskReserve, "q_creator_s_eq", creator,"q_tskNum_s_eq",tskNum, "q_deleted_s_eq", 'N');
            ApiResult apiResult = JSONObject.parseObject(result, ApiResult.class);
            return apiResult;
    }
    @RequestMapping(value = "satisfactions", method = GET)
    public String satisfactions(){
        return "usercenter/satisfactions";
    }
    @RequestMapping(value = "appointments", method = GET)
    public String appointments(){
        return "usercenter/appointments";
    }
    @RequestMapping(value = "consults", method = GET)
    public String consults(){
        return "usercenter/consults";
    }
    @RequestMapping(value = "complains", method = GET)
    public String complains(){
        return "usercenter/complains";
    }
    @RequestMapping(value = "logoff", method = GET)
    public String logoff(){
        return "usercenter/logoff";
    }

    @RequestMapping(value = "userinfo", method = GET)
    public String userinfo(Model model, HttpServletRequest request){
        Map mp_current_member = (Map) request.getSession().getAttribute(Constant.CURRENT_MEMBER);
        String result = HttpClientUtil.doGet(configInfo.getMsHostUrl() + "members/"+mp_current_member.get("guid"));
        ApiResult apiResult = JSONObject.parseObject(result, ApiResult.class);
        String dateString=apiResult.getData().toString();
        Member mb= JSONObject.parseObject(dateString, Member.class);
        if(StringUtils.isNotBlank(mb.getIdNum())) {
            String idCode = mb.getIdNum().substring(0, 4) + "*************" + mb.getIdNum().substring(16);
            mb.setIdNum(idCode);
        }
        model.addAttribute("usr",mb);

        String result2 = HttpClientUtil.doGet(configInfo.getMsHostUrl() + "members/"+mp_current_member.get("guid")+"/att");
        ApiResult apiResult2 = JSONObject.parseObject(result2, ApiResult.class);
        if(apiResult2.getData()==null)
            model.addAttribute("pic","");
        else
            model.addAttribute("pic",apiResult2.getData().toString());

        String result3 = HttpClientUtil.doGet(configInfo.getMsHostUrl() + "comps/"+mp_current_member.get("guid"));
        ApiResult apiResult3 = JSONObject.parseObject(result3, ApiResult.class);
        if(apiResult3.getData()==null)
            model.addAttribute("qy","");
        else {
            CompInfo ci= JSONObject.parseObject(apiResult3.getData().toString(), CompInfo.class);
            model.addAttribute("qy", ci);
        }


        return "usercenter/userinfo";
    }

    @RequestMapping(value="usermessage",method = GET)
    public String usermessage(Model model, HttpServletRequest request)
    {

        return  "usercenter/message";
    }




    @RequestMapping(value = "{guid}",method = RequestMethod.PATCH)
    @ResponseBody
    public ApiResult patchTask(@PathVariable String guid, @RequestBody Map map){
        String result = HttpClientUtil.doPatch(configInfo.getMsHostUrl() + "tasks/"+guid,map, ContentType.APPLICATION_JSON);
        ApiResult apiResult = JSONObject.parseObject(result, ApiResult.class);
        return apiResult;
    }


}
